﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MC.RideEvent.Mvc.Models.Entity;
using MC.RideEvent.Mvc.Models.ViewModel;
using AutoMapper;

namespace MC.RideEvent.Mvc.Models.Repository
{
    public class EventRepository : IEventRepository
    {
        #region Constantes
        private const int THE_LAST = 8;
        private const int INTERVAL_DATE_END = 30;
        #endregion

        #region Fields
        private RideEventDBEntities _context;
        private DateTime DateNow
        {
            get { return DateTime.Now;  }
        }

        private DateTime DateNowEnd
        {
            get { return DateTime.Now.AddDays(INTERVAL_DATE_END); }
        }
        #endregion

        #region Constructors
        public EventRepository()
        {
            _context = new RideEventDBEntities();
        }
        #endregion

        #region Methods

        public List<EventViewModel> GetEvents()
        {
            var list = (from evt in _context.Events
                       where evt.Activated
                       where evt.DateBegin >= DateNow
                       where evt.DateBegin <= DateNowEnd
                       orderby evt.DateBegin descending
                       select evt).ToList();

            var datas = Mapper.Map<List<Event>, List<EventViewModel>>(list);

            return datas;
        }

        public List<EventViewModel> GetLastEvents()
        {
            var dateNow = DateTime.Now;
            var list = (from evt in _context.Events
                       where evt.Activated
                       where evt.DateBegin > dateNow
                       orderby evt.DateBegin descending
                       select evt).Take(THE_LAST).ToList();

            var datas = Mapper.Map<List<Event>, List<EventViewModel>>(list);

            return datas;
        }

        public List<EventViewModel> ResearchEvents(string text, DateTime? date)
        {
            var list = (from evt in _context.Events
                        where evt.Activated
                        where (evt.Title.Contains(text)
                        || evt.NamePlace.Contains(text)
                        || evt.City.Contains(text))
                        where evt.DateBegin >= DateNow
                        select evt).ToList();

            var datas = Mapper.Map<List<Event>, List<EventViewModel>>(list);

            if (date != null)
            {
                var dateTemp = (DateTime) date;
                datas = datas.Where(evt => evt.DateBegin >= dateTemp && evt.DateEnd >= dateTemp).ToList();
            }

            return datas;
        }

        public bool SuggestEvent()
        {
            bool result = false;

            return result;
        }

        public EventViewModel GetEventById(int id)
        {
            var item = (from evt in _context.Events
                        where evt.Id == id
                        where evt.Activated
                        select evt).FirstOrDefault();

            var data = Mapper.Map<Event, EventViewModel>(item);

            return data;
        }

        public bool IfUserWayExist(int userId, int eventId)
        {
            var way = (from w in _context.Ways
                       where w.DriverId == userId
                       where w.EventId == eventId
                       select w).FirstOrDefault();

            return way != null;
        }

        #endregion
    }
}